<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <title>Documenet</title>
        <script>
            function abc(){
                var oldstr = document.getElementById('old');
                var nwestr = document.getElementById('new');
                var reg = /^\s*|\s$/g

                nwestr.varlue = oldstr.varlue.replace(reg,'');
            }

        </script>
        
    </head>
    <body>
        过滤前:<input type="text" id="old"><button onclick="abc()">过滤</button>

        过滤后:<input type="text" id="new">
    </body>
</html>